package com.sbio.udiapp.receiver;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;

import java.io.File;

public class AlarmReceiver extends BroadcastReceiver {
    private static final String TAG = "AlarmReceiver";

    @Override
    public void onReceive(Context context, Intent intent) {
        Log.d(TAG, "Alarm received");

        // 检查文件是否存在
        File fileToUpload = new File(context.getFilesDir(), "your_file_name.txt");
        if (fileToUpload.exists()) {
            // 执行上传操作（假设你已经有了上传文件的方法）
            boolean uploadSuccess = uploadFileToServer(fileToUpload);
            if (uploadSuccess) {
                // 如果上传成功，则删除本地文件
                boolean deleteSuccess = fileToUpload.delete();
                if (deleteSuccess) {
                    Log.d(TAG, "File deleted after successful upload");
                } else {
                    Log.e(TAG, "Failed to delete file after successful upload");
                }
            }
        } else {
            Log.d(TAG, "File does not exist");
        }
    }

    private boolean uploadFileToServer(File file) {
        // 实现文件上传到服务器的方法
        // 可以使用OkHttp或其他HTTP库发送HTTP请求
        return false; // 这里假设上传失败，实际情况需要根据具体实现修改
    }
}
